package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.lang.Result;
import com.example.entity.ComRecruit;
import com.example.entity.Recruit;
import com.example.service.RecruitService;
import com.example.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cyk
 * @since 2022-02-19
 */
@RestController
public class RecruitController {

    @Autowired
    RecruitService recruitService;

    /**
     * 查询所有招聘信息
     * @return
     */
    @GetMapping("/recruitAll")
    @ResponseBody
    public Result recruitAll(@RequestParam(defaultValue = "1") Integer currentPage){

        PageUtils page = new PageUtils();
        List<ComRecruit> list = recruitService.getAllRecruit();
        IPage pageData = page.getcomRecruitPage(currentPage,5,list);

        return Result.succ(pageData);
    }

    /**
     * 根据招聘信息id查询招聘信息与所发布的公司
     * @param id
     * @return
     */
    @GetMapping("/recruitAll/{id}")
    @ResponseBody
    public Result recruitAllById(@PathVariable(name = "id") Long id){

        ComRecruit list = recruitService.getAllRecruitById(id);

        return Result.succ(list);
    }

    /**
     * 根据招聘信息id查询招聘信息，用于删除招聘信息
     * @param id
     * @return
     */
/*    @GetMapping("/recruit/{id}")
    public Result delete(@PathVariable(name = "id") Long id){

        Recruit recruit = recruitService.getById(id);
        Assert.notNull(recruit,"该招聘信息已被删除");

        return Result.succ(recruit);
    }*/

    /**
     * 根据企业id查询招聘信息，企业用户后台查看自己发布的招聘信息
     * @param id
     * @return
     */
    @GetMapping("/rec/{com_id}")
    public Result detail(@PathVariable(name = "com_id")Long id, @RequestParam(defaultValue = "1") Integer currentPage){

        Page page = new Page(currentPage, 5);
        QueryWrapper queryWrapper = new QueryWrapper<Recruit>().eq("com_id",id);
        IPage recruit = recruitService.page(page, queryWrapper);

        return Result.succ(recruit);
    }

    /**
     * 根据id查询招聘信息
     * @param id
     * @return
     */
    @GetMapping("/queryRecruit/{id}")
    public Result recruit(@PathVariable(name = "id")Long id){
        Recruit recruit = recruitService.getQueryRecruit(id);
        return Result.succ(recruit);
    }

    /**
     * 删除招聘信息
     * @param id
     * @return
     */
    @GetMapping("/delRecruit/{id}")
    public Result recruitDel(@PathVariable(name = "id")Long id){
        int i = recruitService.getDelRecruit(id);
        if (i > 0){
            return Result.succ(200,"操作成功！",null);
        } else {
            return Result.succ(200,"操作失败！",null);
        }
    }

    /**
     * 编辑
     * @param recruit
     * @return
     */
    //@RequiresAuthentication
    @PostMapping("/recruitEdit")
    public Result recruitEdit(@Validated @RequestBody Recruit recruit){

        Recruit temp = new Recruit();
        temp.setId(recruit.getId());
        temp.setComId(recruit.getComId());
        temp.setPost(recruit.getPost());
        temp.setNumber(recruit.getNumber());
        temp.setSalary(recruit.getSalary());
        temp.setMajor(recruit.getMajor());
        temp.setContent(recruit.getContent());
        temp.setReTime(LocalDateTime.now());

        if (temp.getId() != null){
            // 编辑
            boolean b = recruitService.updateRecruit(temp);
            return Result.succ(200,"修改成功！",null);
        } else {
            // 添加
            //BeanUtils.copyProperties(recruit,temp,"id", "comId", "post", "number", "salary", "major", "content");
            boolean b = recruitService.saveRecruit(temp);
            return Result.succ(200,"添加成功！",null);
        }

    }

}
